<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本序列动画（SplitText.create版）</title>
    <style>
        body {
            font-family: 'Helvetica Neue', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 0;
            color: white;
        }
        .container {
            max-width: 800px;
            padding: 2rem;
            text-align: center;
        }
        .sequence-text {
            font-size: clamp(1.75rem, 4vw, 2.5rem);
            font-weight: 700;
            line-height: 1.3;
            margin-bottom: 2rem;
        }

        .line {
            display: block;
            overflow: hidden;
            margin-bottom: 0.5em;
            font-size: 2.5rem;
            font-weight: 300;
            line-height: 1.4;
        }
        .word {
            display: inline-block;
            opacity: 0;
            transform: translateY(100%);
        }
        .btn {
            padding: 0.8rem 1.5rem;
            background: transparent;
            color: white;
            border: 2px solid white;
            border-radius: 50px;
            cursor: pointer;
            font-size: 1rem;
            margin-top: 2rem;
            transition: all 0.3s;
        }
        .btn:hover {
            background: white;
            color: #764ba2;
        }
    </style>
</head>
<body>
<div class="container">
    <div id="text" class="sequence-text">
        <div>这是第一行文本</div>
        <div>动画会按行依次显示</div>
        <div>每行的单词逐个出现</div>
        <div>创造流畅的阅读体验</div>
    </div>
    <button class="btn" id="replay">重新播放</button>
</div>

<!-- 无需手动注册，可直接使用 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/SplitText.min.js"></script>
<script>
    // 注册插件
    gsap.registerPlugin(SplitText);

    let splits = [];
    let masterTL;

    function initAnimation() {
        // 清除现有动画和分割
        if (masterTL) masterTL.kill();
        splits.forEach(split => split.revert());
        splits = [];

        // 使用SplitText.create()处理每行文本
        const lines = document.querySelectorAll("#text div");
        lines.forEach((line, index) => {
            const split = SplitText.create(line, {
                type: "words",
                wordsClass: "word",
                onSplit: (self) => {
                    // 设置初始状态
                    gsap.set(self.words, {
                        opacity: 0,
                        y: "100%"
                    });

                    // 添加到分割实例数组
                    splits.push(self);

                    // 如果是第一行，创建主时间轴
                    if (index === 0) {
                        masterTL = gsap.timeline();
                    }

                    // 为每行添加动画到时间轴
                    masterTL.to(self.words, {
                        duration: 0.8,
                        opacity: 1,
                        y: "0%",
                        stagger: 0.1,
                        ease: "back.out"
                    }, index * 0.5); // 行间延迟
                }
            });
        });
    }

    document.addEventListener('DOMContentLoaded', initAnimation);
    document.getElementById("replay").addEventListener("click", initAnimation);
</script>
</body>
</html>